
module WindowNewCustomer
(
    newCustomerWindowAcid
) 
where

import Graphics.UI.Gtk
import Data.IORef
import Customer
import MainHandler

  
newCustomerWindowAcid state combo = do 
  initGUI
  window <- windowNew
  windowSetDefaultSize window 150 150
  windowSetPosition window WinPosCenter
  windowSetTitle window "Nuevo cliente"
  
  
  
  hbox <- vBoxNew True 5
  entryName <- labelNew (Just "Nombre de Cliente Nuevo:")
  entry <- entryNew
  
  buttonCtm <- buttonNewWithLabel "Agregar cliente"
  buttonCtm `onClicked` addNewCustomerAcid state entry combo (Just window)
  
  set window [windowDefaultWidth := 150, windowDefaultHeight := 150,
              containerBorderWidth := 10, containerChild := hbox]
              
  boxPackStart hbox entryName PackNatural 0                 
  boxPackStart hbox entry PackNatural 0            
  boxPackStart hbox buttonCtm PackNatural 0
  
  
  widgetShowAll window
 
------------------- FUNCIONES CON ACID -------------------------------------------- 
addNewCustomerAcid state entry combo win = do 
    aName <- entryGetText entry
    
    --GUARDO EL CUSTOMER NUEVO
    addCustomerAcid state (createCustomer aName)
    
    addMsg <- messageDialogNew win [DialogDestroyWithParent] MessageInfo ButtonsOk "Cliente agregado"
    dialogRun addMsg
    
    let w = case win of 
                (Just x) -> x
    widgetDestroy w
    updateComboAcid combo state
    return ()

updateComboAcid combo state = do
    customers <- getCustomersAcid state
    store <- listStoreNew (customers)
    comboBoxSetModel combo (Just store)
    customStoreSetColumn store textColumn (getCustomerName)


    
textColumn :: ColumnId Customer String
textColumn = makeColumnIdString 0